﻿using System;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using AmazeUI.bll.GetModel;
using AmazeUI.db;
using AmazeUI.Imodel;
using WebCommon;

namespace AmazeUI.bll
{
    public class XtUserBll : BaseBll
    {
        /// <summary>
        /// 筛选
        /// </summary>
        /// <returns></returns>
        public IQueryable<XtUser> Where()
        {
            IQueryable<XtUser> xtUsers = this.db.XtUser.Where(a => a.IsUsing);
            return xtUsers;
        }

        public PageListJs<XtUser2> GetPage(int rows, int page, string wdname)
        {
            var xtUsers = Where().Where(a => a.Name.Contains(wdname));
            var pj = new PageListJs<XtUser>(xtUsers, rows, page);
            return pj.OpData(WebCommon.StringCommand.Static.ParentToSub<XtUser, XtUser2>);
         
        }

        /// <summary>
        /// 登录账号密码
        /// </summary>
        /// <param name="username"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public XtUser Login(string username, string pwd)
        {
            var md5Pwd = ComBll.Md5(pwd);
            var user = this.Where().FirstOrDefault(a => a.UserName == username && a.Pwd == md5Pwd);
            return user;
        }

        /// <summary>
        /// 写入登录信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        public bool LoginEna(int id, string ip)
        {
            var user = this.Where().FirstOrDefault(a => a.Id == id);
            if (user != null)
            {
                var en = user.EnaTime > DateTime.Now;
                var di = user.DisTime < DateTime.Now;
                var b = en && di;
                if (b)
                {
                    user.LogNum += 1;
                    user.LogTime = DateTime.Now;
                    user.UsingIp = ip;
                    this.db.SaveChanges();
                }
                return b;
            }
            else
            {
                return false;
            }

        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddXtUser(IXtUser model)
        {
            var user = this.db.XtUser.FirstOrDefault(a => a.UserName == model.UserName);
            if (user!=null)
            {
                return user.Id;
            }

            model.Pwd = string.IsNullOrWhiteSpace(model.Pwd) ? WebCommon.StringCommand.RandomsAzNum.GetpUn(16) : model.Pwd;
            model.Pwd = ComBll.Md5(model.Pwd);
         

            var xu = new XtUser(model);

            this.db.XtUser.Add(xu);
            this.db.SaveChanges(); 
        
          
            return xu.Id;
        }


        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pwd">旧</param>
        /// <param name="npwd">新</param>
        /// <returns></returns>
        public bool EditPwd(int id, string pwd, string npwd)
        {

            var md5 = ComBll.Md5(pwd);
            var xtUser = this.db.XtUser.FirstOrDefault(a => a.Id == id && md5 == a.Pwd);
            if (xtUser != null)
            {
                xtUser.Pwd = ComBll.Md5(npwd);

                this.db.SaveChanges();
                return true;
            }
            else
            {
                return false;
            }
        }

        public XtUser GetModel(int id)
        { 
            return this.Where().FirstOrDefault(a => a.Id == id);
             
        }
    }
}
